const instance = {
    cart: JSON.parse(localStorage.getItem('cart')) || [],
    totalCount: 0,
    totalMoney: 0,
    allChecked: false
}

export function cartReducer(state = instance, action) {
    switch(action.type) {
        // 添加进购物车
        case 'ADDCART':
            // 声明变量控制添加进购物车 true为不重复 false为重复
            var rel = true;
            // 判断是否重复 是count++ 变量变为false
            state.cart.forEach(element=> {
                if(element.item._id === action.item._id) {
                    element.count++
                    rel = false;
                    return
                }
            })
            // 如果rel为true 变量未重复 添加进购物车
            if(rel) {
                state.cart.push({
                    count: 1,
                    checked: false,
                    item: action.item
                })
            }
            // 保存cart
            localStorage.setItem('cart', JSON.stringify(state.cart))
            return {
                cart: state.cart,
                totalCount: state.totalCount,
                totalMoney: state.totalMoney,
                allChecked: state.allChecked
            };
            // 选中单个商品计算总数总价
            case 'SELSECT':
                // 通过—id判断哪个商品被选中
                state.cart.forEach(item=> {
                    if(item.item._id === action.item._id) {
                        item.checked = action.item.checked
                    }
                })
                // 如果被选中计算总数总价
                state.totalCount = 0;
                state.totalMoney = 0
                state.cart.forEach(item=> {
                    if(item.checked) {
                        state.totalCount += item.count;
                        state.totalMoney += item.count * item.item.salePrice
                    }
                })
                // 全部单个商品选中全选高亮
                state.allChecked = state.cart.every(item=> {
                    return item.checked === true
                })
                localStorage.setItem('cart', JSON.stringify(state.cart))
                return {
                    cart: state.cart,
                    totalCount: state.totalCount,
                    totalMoney: state.totalMoney,
                    allChecked: state.allChecked
                };
            // 全部选中
            case 'ALLSELSECT':
                // 点击全选全部商品高亮
                state.allChecked = action.checked
                state.cart.forEach(item=> {
                    item.checked = action.checked
                })
                // 计算总数总价
                state.totalCount = 0;
                state.totalMoney = 0
                state.cart.forEach(item=> {
                    if(item.checked) {
                        state.totalCount += item.count;
                        state.totalMoney += item.count * item.item.salePrice
                    }
                })
            localStorage.setItem('cart', JSON.stringify(state.cart))
            return {
                cart: state.cart,
                totalCount: state.totalCount,
                totalMoney: state.totalMoney,
                allChecked: state.allChecked
            };
        default:
            return state;
    }
}